<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/login.css" />
  <title>Document</title>
</head>

<body>
  <div class="loginContainer">
    <h1>登录</h1>
    <form action="/checkUser" method="post">姓名：
      <input class="inputStyle" type="text" name="username" />
      <div class="exchange">用户名错误</div>
      <br />密码：
      <input class="inputStyle" type="password" name="pwd" /><br />
      <input class="loginStyle" type="submit" value="登录" />
    </form>
  </div>
  <script>
    // ajax技术；
    // XMLHttpRequest
    document.querySelector(".inputStyle").onblur = function () {
      // console.log(this.value);
      let xhr = new XMLHttpRequest();
      xhr.open("get", `/checkUserName?username=${this.value}`, true);
      xhr.onload = function () {
        console.log(xhr.responseText);
        let obj = JSON.parse(xhr.responseText);
        document.querySelector(".exchange").innerHTML = obj.info;
        if (obj.status == 1) {
          document.querySelector(".exchange").style.color = "green";
        } else {
          document.querySelector(".exchange").style.color = "red";
        }
      }
      xhr.send();
    }


  </script>
</body>